package Lab12;

import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class _4 {
    public static void main(String[] args) throws IOException {
        String content="";
        try{
            URL url = new URL("https://www.douban.com/robots.txt");
            URLConnection coo = url.openConnection();
            coo.setRequestProperty("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/61.0.3163.100 Safari/537.36)");
            InputStream is = coo.getInputStream();
            ByteArrayOutputStream outStream = new ByteArrayOutputStream();

            byte[] bs = new byte[1024];
            int len;
            while((len = is.read(bs))!=-1)
                outStream.write(bs,0,len);
            byte[] bb =outStream.toByteArray();
            content = new String(bb);
            char fx = 'A';
            int w=0;
            for(int i=0;i<content.length();i++)
            {
                if(!(content.charAt(i)>='a'&&content.charAt(i)<='z')&&!(content.charAt(i)>='A'&&content.charAt(i)<='Z')&&(fx>='a'&&fx<='z'))
                    w++;
                fx=content.charAt(i);
            }
            System.out.println("The number of words is "+w);
//            The number of words is 99
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}